package models
import (
	"time"
	"github.com/astaxie/beego/orm"
	."LiquidMeasurement/lib"
	"github.com/astaxie/beego"
	"fmt"
	"strconv"
)
type Admin struct {
	Tel				string		`valid:"Mobile"`
	UserID          int64		`orm:"unique;auto"`
	JobID			string		`orm:"unique;size(200)" "PK" form:"JobID"  valid:"Required;MaxSize(200);MinSize(11)"`
	UserName    	string		`orm:"size(100)" form:"Username"  valid:"Required;MaxSize(100);MinSize(6)"`
	Passwd			string		`orm:"size(100)" form:"Password" valid:"Required;MaxSize(20);MinSize(6)"`
	Permission    	int8        //权限位，0x01&Permission == 1,代表管理员权限	//Group     		*Group   `orm:"rel(one)"` // OneToOne relation
	Createtime    	time.Time `orm:"type(datetime);auto_now_add" `
	Updatetime    	time.Time `orm:"type(datetime);auto_now_add" `
	IdentifyCard        string
	Department			*Department		`orm:"rel(fk)"`
	FilterDepartmentID	uint64		`orm:"default(0)"`
	FilterFloor			uint64		`orm:"default(0)"`
}


//type Filter struct  {
//	Id  uint64			`orm:"unique;auto"`
//	DepartmentID  uint64
//	Floor  uint64
//
//
//}

type ResponeAdminModel struct {
	Tel				string		`valid:"Mobile"`
	UserID          int64		`orm:"unique;auto"`
	JobID			string		`orm:"unique;size(200)" "PK" form:"JobID"  valid:"Required;MaxSize(200);MinSize(11)"`
	UserName    	string		`orm:"size(100)" form:"Username"  valid:"Required;MaxSize(100);MinSize(6)"`
	Passwd			string		`orm:"size(100)" form:"Password" valid:"Required;MaxSize(20);MinSize(6)"`
	Permission    	int8        //权限位，0x01&Permission == 1,代表管理员权限	//Group     		*Group   `orm:"rel(one)"` // OneToOne relation
	Createtime    	time.Time `orm:"type(datetime);auto_now_add" `
	Updatetime    	time.Time `orm:"type(datetime);auto_now_add" `
	IdentifyCard        string
	Department			Department		`orm:"rel(fk)"`
}


func init() {
	// 需要在init中注册定义的model
	orm.RegisterModel(new(Admin))

}


//获取本服务器的连接池中的设备
//@return   admins []Admin   返回admin的数组
//@return   count int64      数组的数量

func GetAdminList ()(respone_admins []ResponeAdminModel,count int64) {
	o := orm.NewOrm()
	admin := new(Admin)
	qs := o.QueryTable(admin)
	count, _ = qs.Count()
	admins := make([]Admin,count)
	respone_admins = make([]ResponeAdminModel,count)
	qs.All(&admins)
	for index, value := range admins {
		department ,_ := GetDepartmnetById(value.Department.Id)
		respone_admins[index].Department = department
		respone_admins[index].UserID = value.UserID
		respone_admins[index].Createtime = value.Createtime
		respone_admins[index].IdentifyCard = value.IdentifyCard
		respone_admins[index].JobID = value.JobID
		respone_admins[index].Passwd = value.Passwd
		respone_admins[index].Permission = value.Permission
		respone_admins[index].Tel = value.Tel
		respone_admins[index].Updatetime = value.Updatetime
		respone_admins[index].UserName = value.UserName


	}
	return respone_admins, count
}

//添加用户
//@param u  *Admin      要添加的用户数组结构体
//@return    int64      返回用户的id
//@return    error      返回的错误
func AddAdmin(u *Admin) (int64, error) {
	o := orm.NewOrm()

	u.Passwd = Strtomd5(u.Passwd)
	id, err := o.Insert(u)
	return id, err
}
//通过jobID获取用户数据
//@param 	jobID  string      用户的电话号码
//@return   user Admin      返回用户的数组
//@return   err	 error      返回的错误
func GetAdminByJobID(jobID string) (user Admin,err error) {
	user = Admin{JobID: jobID}
	o := orm.NewOrm()
	err = o.Read(&user, "job_i_d")
	if err == orm.ErrNoRows {
		beego.Info("查询不到")
	} else if err == orm.ErrMissPK {
		beego.Info("找不到主键")
	}
	fmt.Println(user.JobID,user.Passwd)
	beego.Info("user get admin",user)
	return user,err
}
//通过userID获取用户数据
//@param 	userID   int64      用户的用户ID
//@return   user Admin      返回用户的数组
//@return   err	 error      返回的错误
func GetAdminByUserID(userID int64) (user Admin,err error) {
	user = Admin{UserID: userID}
	o := orm.NewOrm()
	err = o.Read(&user, "user_i_d")
	if err == orm.ErrNoRows {
		beego.Info("查询不到")
	} else if err == orm.ErrMissPK {
		beego.Info("找不到主键")
	}
	beego.Info("user get admin",user)
	return user,err
}
//通过userID获取用户数据
//@param 	userID   int64      用户的用户ID
//@return   user Admin      返回用户的数组
//@return   err	 error      返回的错误
func GetAdminByUserIDS(UserID string) (user Admin,err error) {
	userID,_:=strconv.ParseInt(UserID,10,64)
	user = Admin{UserID: userID}
	o := orm.NewOrm()
	err = o.Read(&user, "user_i_d")
	if err == orm.ErrNoRows {
		beego.Info("查询不到")
	} else if err == orm.ErrMissPK {
		beego.Info("找不到主键")
	}
	beego.Info("user get admin",user)
	return user,err
}
//通过userID删除用户数据
//@param 	userID   int64      用户的用户ID
//@return   	 	int64      返回删除数据的状态
//@return   err	 	error      返回的错误
func DelAdminByUserID(userID int64) (int64, error) {
	o := orm.NewOrm()
	status, err := o.Delete(&Admin{UserID: userID})
	return status, err
}
//通过jobID删除用户数据
//@param 	jobID   string      用户的工号
//@return   	 	int64      返回删除数据的状态
//@return   err	 	error      返回的错误
func DelAdminByJobID(jobID string) (int64, error) {
	admin,err1 := GetAdminByJobID(jobID)
	if err1 != nil{
		return 0, err1
	}
	o := orm.NewOrm()
	status, err := o.Delete(&admin)
	return status, err
}
//更新用户
//@param 	u   Admin      更新用户的数组结构
//@return   	Admin      返回用户的数据
//@return   err	 error      返回的错误
func UpdateAdmin(u *Admin) (*Admin, error) {
	//user := Admin{UserID:u.UserID}
	o := orm.NewOrm()
	u.Updatetime = time.Now()
	_,err := o.Update(u)

	return u, err
}